package com.cxy.autoconfig;

import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: huayushu luming
 * @date: 2020-11-04 18:37
 * @desc:
 **/
@Configuration
@ConditionalOnProperty("elasticjob.zookeeper.server-list")
@EnableConfigurationProperties(ZookeeperProperties.class)
public class ZookeeperAutoConfig {

    @Autowired
    private ZookeeperProperties  zookeeperProperties;
    /**
     * zookeeper注册中心
     * @return
     */
    @Bean(initMethod = "init")
    public  CoordinatorRegistryCenter zkCenter(){
        String serverList=zookeeperProperties.getServerList();
        String namespace=zookeeperProperties.getNamespace();
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(serverList, namespace);
        ZookeeperRegistryCenter zookeeperRegistryCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        //注册中心初始化
       // zookeeperRegistryCenter.init();
        return zookeeperRegistryCenter;


    }
}
